<!--
* description: 包材样式优化-新增编辑
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="sendCode"
      width="1000px"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :readonly="comp.readonly"
            :showPlaceholder="comp.showPlaceholder"
            :dateOptionsType="comp.dateOptionsType"
            :options="comp.options"
            :isInput="comp.isInput"
          />
        </el-row>
        <el-row :gutter="34" style="margin-bottom:5px;">
          <el-col :span="24">
            <div class="dialogTitle">
              <span class="dialogTitleButton">
                <el-button class="iconfont icon-newlyAdded" @click="tableDataAdd">添加包材</el-button>
              </span>
            </div>
          </el-col>
        </el-row>
        <!-- 表格 -->
        <el-row :gutter="34" style="margin-bottom:5px;">
          <el-col :span="24">
            <el-table
              :data="tableData"
              style="width: 100%"
              height="300"
              border
              v-loading="listLoading"
            >
              <el-table-column label="序号" width="60" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="120">
                <template slot-scope="scope">
                  <el-link
                    type="primary"
                    class="el-icon-delete"
                    :underline="false"
                    @click="tableDataDel(scope.$index)"
                    title="删除"
                    style="font-size:12px;"
                  >删除</el-link>
                </template>
              </el-table-column>
              <el-table-column label="包材编号" align="center" prop="MATERIALS_CODE"></el-table-column>
              <el-table-column label="包材名称" prop="MATERIALS_NAME" align="center"></el-table-column>
              <el-table-column label="包材数量+" align="center">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.MATERIALS_NUM"
                    type="text"
                    @input="numChange(scope)"
                    style="width:100%;"
                    class="tableTemplate"
                    maxlength="10"
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <!-- 表格 -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
      <editPackingMaterialStyle
        v-if="isEditShow"
        @PackingMaterialStyleClose="PackingMaterialStyleClose"
        @confirmPackingMaterialStyle="confirmPackingMaterialStyle"
        :isEditShow="isEditShow"
        :editformField="formField"
        :editTableData="tableData"
      />
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import { wmsApis } from "@/api/wms/packingMaterialStyle/api";
import storage from "@/utils/storage";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader"), //弹窗标题
    editPackingMaterialStyle: () => import("./editPackingMaterialStyle")
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: wmsApis.saveMaterialsStyle,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.close("1"),
          text: "关闭"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "备件编号",
          textField: "PART_CODE",
          codeField: "PART_CODE",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfoM"),
          type: "propus",
          isMust: true,
          span: 8,
          isMul: false,
          isRequire: true,
          returnCodeField: "PART_CODE",
          returnTextField: "PART_CODE"
        },
        {
          compKey: "edit_compKey2",
          labelName: "包装数量",
          codeField: "PART_NUM",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          span: 8,
          isInput: true,
          isRequire: true,
          changed: val => {
            this.$refs["trueedit_compKey2"][0].modelCode = (
              this.formField.PART_NUM + ""
            ).replace(/[^\d]/g, "");
            this.formField.PART_NUM = (this.formField.PART_NUM + "").replace(
              /[^\d]/g,
              ""
            );
          }
        },
        {
          compKey: "edit_compKey3",
          labelName: "备注",
          span: 24,
          codeField: "REMARK",
          component: () => import("@/components/wms/textareaInput"),
          isMust: true,
          isRequire: true
        }
      ],
      // 标题
      textMap: {
        add: "新增包材样式优化",
        edit: "编辑包材样式优化"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        MATERIALS_STYLE_ID: "",
        PART_CODE: "",
        PART_NUM: "",
        REMARK: ""
      },
      //表格数据
      tableData: [],
      // 表格加载字段
      listLoading: false,

      // 控制备件明细弹窗
      isEditShow: false
    };
  },
  methods: {
    onCreated() {
      if (this.curPopupsVisible) {
        if (this.popupsState === "edit") {
          this.listLoading = true;
          request({
            url: "/mp/wm/vl/warehouse/materialsStyle/queryMaterialsStyleD.do",
            method: "post",
            data: {
              pageIndex: 1,
              pageSize: 9999,
              MATERIALS_STYLE_ID: this.formField.MATERIALS_STYLE_ID
            }
          })
            .then(res => {
              this.listLoading = false;
              if (res.result === "1") {
                this.tableData =
                  res.listRow && res.listRow.length > 0 ? res.listRow : [];
              } else {
                if (res.msg || res.debugMsg) {
                  this.$message({
                    message: res.debugMsg ? res.debugMsg : res.msg,
                    uration: 2000
                  });
                }
              }
            })
            .catch(err => {
              console.info(err);
              this.listLoading = false;
            });
        }
      }
    },
    confirmPackingMaterialStyle(checkList) {
      this.isEditShow = false;
      checkList.forEach(item => {
        this.tableData.push(item);
      });
    },
    // 采购数量change事件
    numChange(scope) {
      if (scope.row.PART_NUM < 1) {
        scope.row.PART_NUM = "";
      }
      scope.row.PART_NUM = (scope.row.PART_NUM + "").replace(/[^\d]/g, "");
    },
    // 新增按钮 点击事件
    tableDataAdd() {
      this.isEditShow = true;
    },
    PackingMaterialStyleClose() {
      this.isEditShow = false;
    },
    //  删除按钮 点击事件
    tableDataDel(index) {
      this.$confirm("您确定是否要进行此操作？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true
      })
        .then(() => {
          this.tableData.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {});
    },
    //保存
    save() {
      let flag = false;
      if (this.tableData.length < 1) {
        this.$message({
          message: "至少保存一条包材明细"
        });
        return;
      }
      this.tableData.forEach(item => {
        if (!item.MATERIALS_NUM) {
          flag = true;
        }
      });
      if (flag) {
        this.$message({
          message: "明细中的包装数量未输入"
        });
        return;
      }
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(this.formField));
        saveObj.STYLE_D = this.tableData;
        //保存表单
        this.saveForm(saveObj);
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" lang="scss" scoped>
.dialogTitle {
  text-align: left;
  overflow: hidden;
}
.dialogTitleButton {
  float: right;
}
/deep/ .tableTemplate .el-input__inner {
  border: none;
}
.dialogTip {
  color: red;
  margin-right: 10px;
  font-size: 12px;
}
</style>